'use strict';

define(function(){
	var router = function(handler){
		if(!handler){
			console.log('handler 没有设置正确');
			return false;
		}
		this.handler = handler;
	};

	router.prototype.init = function(){
		if(!this.handler.main){
			console.log('handler 中缺少main，没有设置正确，初始化失败');
			return false;
		}
		var handler = this.handler;

		


		window.onhashchange = (function(){
			var hashHandlerFunc = function(){
				var hash = null;
				var argv = {};
				var temp = null;
				if(window.location.hash.split('#')[1]){
					temp = window.location.hash.split('#')[1].split('?');
				}else{
					temp = [];
				}
				if(!temp[0]){
					hash = 'main';
				}
				else{
					//var temp = window.location.hash.split('#')[1].split('?');
					hash = window.location.hash.split('#')[1].split('?')[0];
				}

				if(temp[1]){
					var temp = temp[1].split('&');
					for( var i in temp ){
						argv[temp[i].split('=')[0]] = temp[i].split('=')[1];
					}
				}


				//console.log(argv)
				//console.log((typeof handler[hash]));
				if(typeof handler[hash] === 'function'){
					handler[hash](argv);
				}
			};
			hashHandlerFunc();
			return hashHandlerFunc;
		})();

	}

	return router;
});